package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.service.MessageService;
import com.the4man.cnbdw.utils.PageUtil;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.service.UserInfoServiceI;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Action(
        value = "messageAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/message/add.jsp"
        ), @Result(
                name = "addMsgPage",
                location = "/cnbdw/message/addMsg.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/message/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/message/edit.jsp"
        )}
)
public class MessageAction extends BaseAction implements ModelDriven<Message> {
    private Message model = new Message();
    @Autowired
    private MessageService messageService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private UserInfoServiceI userInfoService;

    public MessageAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toAddMsgPage() {
        return "addMsgPage";
    }

    public void getAllDatasourceList() {
        int currentPage = this.getPage();
        int pageSize = this.getRows();
        HttpServletRequest request = this.getRequest();
        String name = request.getParameter("name");
        String keyWord = request.getParameter("keyWord");
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        DataGrid d = new DataGrid();

        try {
            int cnt = this.messageService.getMsgCnt(keyWord);
            PageUtil page = PageUtil.build(currentPage, pageSize, cnt);
            List<Message> list = this.messageService.getMsgPage(page.getStartRows(), page.getPageSize(), keyWord, name, start, end);
            d.setRows(list);
            d.setTotal(Long.valueOf((long)cnt));
            this.writeJson(d);
        } catch (Exception var12) {
            d.setRows((List)null);
            d.setTotal(Long.valueOf(0L));
            this.writeJson(d);
            LOG.error(var12.getMessage(), new String[0]);
        }

    }

    public void adminSendMessage() {
        new EmployeeExample();
        HttpServletRequest request = this.getRequest();
        String msgType = request.getParameter("msgType");
        String timing = request.getParameter("timing");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date datetime = null;

        try {
            datetime = sdf.parse(timing);
        } catch (ParseException var15) {
            var15.printStackTrace();
        }

        Date now = new Date();
        String createUser = this.getSessionUserId();
        PageJson resultJson = new PageJson();
        List<String> recIdList = new ArrayList();
        if(Integer.valueOf(msgType).intValue() == 0) {
            String recUserIds = request.getParameter("recUser");
            if(recUserIds == null || recUserIds.length() < 1) {
                resultJson.setReturnFlag(false);
                resultJson.setReturnMsg("请选择接受人员!");
                this.writeJson(resultJson);
                return;
            }

            String[] recuser = recUserIds.split(",");

            for(int i = 0; i < recuser.length; ++i) {
                recIdList.add(recuser[i]);
            }

            if(now.getTime() > datetime.getTime() + 60000L) {
                resultJson.setReturnFlag(false);
                resultJson.setReturnMsg("发送时间已过!");
                this.writeJson(resultJson);
                return;
            }
        } else {
            try {
                List<Employee> empList = this.employeeService.selectByPunch((String)null);
                Iterator var13 = empList.iterator();

                while(var13.hasNext()) {
                    Employee employee = (Employee)var13.next();
                    recIdList.add(employee.getId());
                }
            } catch (Exception var16) {
                var16.printStackTrace();
            }
        }

        Message message = this.getModel();
        message.setMsgId(UUID.randomUUID().toString());
        message.setMsgCreateTime(new Date());
        message.setMsgCreateUser(createUser);
        message.setSilent("0");
        message.setMsgType(msgType);
        message.setMsgSendTime(timing);

        try {
            boolean flag = this.messageService.addSendMessage(message, recIdList);
            if(flag) {
                resultJson.setReturnFlag(true);
                resultJson.setReturnMsg("发送成功!");
            }
        } catch (Exception var14) {
            var14.printStackTrace();
            resultJson.setReturnMsg(var14.getMessage());
        }

        this.writeJson(resultJson);
    }

    public void toAddMsg() {
        HttpServletRequest request = this.getRequest();
        String content = request.getParameter("msgContent");
        PageJson resultJson = new PageJson();
        Message message = this.getModel();
        message.setMsgId(UUID.randomUUID().toString());
        message.setMsgCreateTime(new Date());
        message.setMsgContent(content);

        try {
            int i = this.messageService.addMsg(message);
            if(i > 0) {
                resultJson.setReturnFlag(true);
                resultJson.setReturnMsg("添加成功!");
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            resultJson.setReturnMsg(var6.getMessage());
        }

        this.writeJson(resultJson);
    }

    public void delete() {
        HttpServletRequest request = this.getRequest();
        String ids = request.getParameter("ids");
        List<String> msgIdList = new ArrayList();
        String[] idArray = ids.split(",");
        String[] var8 = idArray;
        int var7 = idArray.length;

        for(int var6 = 0; var6 < var7; ++var6) {
            String id = var8[var6];
            msgIdList.add(id);
        }

        PageJson resultJson = new PageJson();

        try {
            boolean flag = this.messageService.deleteMessage(msgIdList);
            if(flag) {
                resultJson.setReturnFlag(true);
                resultJson.setReturnMsg("删除成功!");
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            resultJson.setReturnMsg("删除失败!");
        }

        this.writeJson(resultJson);
    }

    public void getEmpJSON() {
        List<Map<String, String>> list = new ArrayList();
        List empList = null;

        try {
            empList = this.employeeService.allHaveDeviceEmp();
        } catch (Exception var6) {
            var6.printStackTrace();
            return;
        }

        Iterator var4 = empList.iterator();

        while(var4.hasNext()) {
            Employee employee = (Employee)var4.next();
            Map<String, String> map = new HashMap();
            map.put("id", employee.getId());
            map.put("text", employee.getName());
            list.add(map);
        }

        this.writeJson(list);
    }

    public void getMsgJSON() {
        List<Map<String, String>> list = new ArrayList();
        List msg = null;

        try {
            msg = this.messageService.selectALLMsg();
        } catch (Exception var6) {
            var6.printStackTrace();
            return;
        }

        Iterator var4 = msg.iterator();

        while(var4.hasNext()) {
            Message message = (Message)var4.next();
            Map<String, String> map = new HashMap();
            map.put("id", message.getMsgId());
            map.put("text", message.getMsgContent());
            list.add(map);
        }

        this.writeJson(list);
    }

    public void getRepllyJSON() {
        List<Map<String, String>> list = new ArrayList();
        List replyList = null;

        try {
            String msgid = this.getRequest().getParameter("msgId");
            replyList = this.messageService.getRepllyList(msgid);
        } catch (Exception var8) {
            var8.printStackTrace();
            return;
        }

        Iterator var4 = replyList.iterator();

        while(var4.hasNext()) {
            MessageReply item = (MessageReply)var4.next();
            HashMap map = new HashMap();

            try {
                Employee emp = (Employee)this.employeeService.selectByPrimaryKey(item.getReplyUser(), Employee.class);
                map.put("empName", emp.getName());
            } catch (Exception var7) {
                var7.printStackTrace();
            }

            map.put("id", item.getReplyId());
            map.put("text", item.getReplyMessag());
            list.add(map);
        }

        this.writeJson(list);
    }

    public Message getModel() {
        return this.model;
    }
}
